package com.buka.goods.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.buka.goods.mapper.SpecMapper;
import com.buka.goods.service.SpecService;
import com.buka.server.pojo.GoodsSpec;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    SpecMapper specMapper;

    @Override
    public List<GoodsSpec> findAll() {
        Example example = new Example(GoodsSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete", 0);

        return specMapper.selectByExample(example);
    }

    @Override
    public List<GoodsSpec> findById(Long id) {
        Example example = new Example(GoodsSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);
        criteria.andEqualTo("isDelete", 0);
        return specMapper.selectByExample(example);
    }

    @Override
    public int addGoodsSpec(GoodsSpec goodsSpec) {
        long id = IdWorker.getId();
        goodsSpec.setId(id);
        int flag = specMapper.insertSelective(goodsSpec);
        return flag;
    }

    @Override
    public int updateGoodsSpec(GoodsSpec goodsSpec) {

        int a = specMapper.updateByPrimaryKeySelective(goodsSpec);
        return a;
    }

    @Override
    public int deleteTrueGoodsSpec(Long id) {
        return 0;
    }

    @Override
    public int deleteFalseGoodsSpec(Long id) {
        Example example=new Example(GoodsSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        GoodsSpec goodsSpec=new GoodsSpec();
        goodsSpec.setIsDelete(1);
        int a=specMapper.updateByExampleSelective(goodsSpec,example);
        return a;
    }

    @Override
    public List<GoodsSpec> FuzzyQuery(String specName) {
        Example example=new Example(GoodsSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete", 0);
        criteria.andLike("specName","%"+specName+"%");
        return specMapper.selectByExample(example);
    }

    @Override
    public Page<GoodsSpec> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Example example = new Example(GoodsSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete", 0);
        return (Page<GoodsSpec>)specMapper.selectByExample(example);
    }

    @Override
    public Page<GoodsSpec> findPage(String specName, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<GoodsSpec>)this.FuzzyQuery(specName);
    }
}
